<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head(${book.id != null ? '编辑图书' : '添加图书'})}"></head>
<body>
    <!-- 导航栏 -->
    <nav th:replace="~{fragments/layout :: navbar}"></nav>
    
    <!-- 页面内容 -->
    <div th:replace="~{fragments/layout :: content(~{::main})}">
        <main>
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card shadow">
                        <div class="card-header bg-primary text-white">
                            <h4 class="my-2" th:text="${book.id != null ? '编辑图书' : '添加新图书'}">添加/编辑图书</h4>
                        </div>
                        <div class="card-body p-4">
                            <form th:action="${book.id != null ? '/books/admin/edit/' + book.id : '/books/admin/add'}" th:object="${book}" method="post">
                                <input type="hidden" th:if="${book.id != null}" th:field="*{id}">
                                
                                <div class="mb-3">
                                    <label for="title" class="form-label">书名</label>
                                    <input type="text" class="form-control" id="title" th:field="*{title}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="author" class="form-label">作者</label>
                                    <input type="text" class="form-control" id="author" th:field="*{author}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('author')}" th:errors="*{author}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="isbn" class="form-label">ISBN</label>
                                    <input type="text" class="form-control" id="isbn" th:field="*{isbn}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('isbn')}" th:errors="*{isbn}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="publicationYear" class="form-label">出版年份</label>
                                    <input type="number" class="form-control" id="publicationYear" th:field="*{publicationYear}" min="1900" th:max="${#calendars.format(#calendars.createNow(), 'yyyy')}">
                                    <div class="text-danger" th:if="${#fields.hasErrors('publicationYear')}" th:errors="*{publicationYear}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="publisher" class="form-label">出版社</label>
                                    <input type="text" class="form-control" id="publisher" th:field="*{publisher}">
                                    <div class="text-danger" th:if="${#fields.hasErrors('publisher')}" th:errors="*{publisher}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="category" class="form-label">分类</label>
                                    <select class="form-select" id="category" th:field="*{category}" required>
                                        <option value="">-- 选择分类 --</option>
                                        <option value="计算机">计算机</option>
                                        <option value="文学">文学</option>
                                        <option value="历史">历史</option>
                                        <option value="科幻">科幻</option>
                                        <option value="教育">教育</option>
                                        <option value="其他">其他</option>
                                    </select>
                                    <div class="text-danger" th:if="${#fields.hasErrors('category')}" th:errors="*{category}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="description" class="form-label">图书简介</label>
                                    <textarea class="form-control" id="description" th:field="*{description}" rows="4"></textarea>
                                    <div class="text-danger" th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></div>
                                </div>
                                
                                <div class="row mb-3">
                                    <div class="col-md-6">
                                        <label for="quantity" class="form-label">总库存数量</label>
                                        <input type="number" class="form-control" id="quantity" th:field="*{quantity}" min="0" required>
                                        <div class="text-danger" th:if="${#fields.hasErrors('quantity')}" th:errors="*{quantity}"></div>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="availableQuantity" class="form-label">可借数量</label>
                                        <input type="number" class="form-control" id="availableQuantity" th:field="*{availableQuantity}" min="0" th:max="*{quantity}">
                                        <div class="text-danger" th:if="${#fields.hasErrors('availableQuantity')}" th:errors="*{availableQuantity}"></div>
                                    </div>
                                </div>
                                
                                <div class="d-flex justify-content-between mt-4">
                                    <a th:href="@{/books/list}" class="btn btn-outline-secondary">
                                        <i class="fas fa-arrow-left"></i> 返回
                                    </a>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save"></i> 保存图书
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
    
    <!-- 页脚 -->
    <footer th:replace="~{fragments/layout :: footer}"></footer>
    
    <!-- 脚本 -->
    <div th:replace="~{fragments/layout :: scripts}"></div>
</body>
</html>
